/*
 * CGlobalSensorTMTCStore.cpp
 *
 *  Created on: 21/07/2013
 *      Author: Santiago Carmona Meco
 */

#include <public/CGlobalSensorTMTCStore.h>


void CGlobalSensorTMStore::set_pack_pool_memory(PackPool * pPackPool,word16_t numOfPackets)
{
	uint16_t i=0;
	uint16_t j=0;
	CPackHandler packAux;

	pPackPool->AllocPackages(numOfPackets,storePackList);

	storePackList.ToHeadPack();
	freePackets=storePackList.GetPackCounter();//puede que sea menos del deseado,
	//por eso se vuelve a comprobar
	for (i=0;i<freePackets;i++)
	{
		storePackList.GetCurrentEditPack(packAux);
		for (j=0;j<256;j++)
		{
			packAux.WriteByte(j,0);
		}
		storePackList.NextEditPack();
	}
	storePackList.ToHeadPack();
	if (storePackList.GetPackCounter())
	{
		emptyPackList=false;
	}
	else
	{
		emptyPackList=true;
	}
	get_free_pack_list(currentPack);

}

void CGlobalSensorTMStore::add_free_pack_list(CPackHandler &pack)
{
	storePackList.AddPack(pack);
	freePackets++;
	emptyPackList=false;
}

bool_t CGlobalSensorTMStore::get_free_pack_list(CPackHandler &pack)
{
	if (freePackets)
	{
		storePackList.TakeOnePack(pack);
		freePackets--;
		return true;
	}
	else
	{
		return false;
	}
}

void CGlobalSensorTMStore::get_tm_pack_list(CPackHandler &pack)
{
	pack=deliveryPack;
}

bool_t CGlobalSensorTMStore::get_force_tm_pack_list(CPackHandler &pack)
{
	uint16_t freeIndex=0;


	freeIndex=get_free_bytes_number();
	if (freeIndex>1)
	{
		for (;freeIndex<254;freeIndex++)
		{
			currentPack.WriteByte(freeIndex,0);
		}
		deliveryPack=currentPack;

		if (!get_free_pack_list(currentPack))
		{
			emptyPackList=true;
		}
		else
		{
			currentPack.WriteByte(0,0);//NF
		}
		get_tm_pack_list(pack);
		return true;
	}
	else
	{
		return false;
	}

}



void CGlobalSensorTMStore::restore_tm_pack_list(CPackHandler &pack)
{
	//	storePackList.AddPack(pack);
}

uint8_t CGlobalSensorTMStore::get_free_bytes_number()
{
	uint8_t nf=0;
	uint8_t freeIndex=1;
	uint8_t length=0;
	uint8_t i=0;


	currentPack.ReadByte(0,nf);

	for (i=0;i<nf;i++)
	{
		currentPack.ReadByte(freeIndex+15,length);
		freeIndex+=18+length;
	}

	return freeIndex;

}


bool_t CGlobalSensorTMStore::store_ssi_tm_frame(byte_t * pFrame)
{

	uint16_t freeIndex=0;
	uint16_t i=0;
	byte_t nf=0;
	uint8_t dataLength= ssi_link_layer_get_data_length(pFrame);



	freeIndex=get_free_bytes_number();

	if (!emptyPackList)
	{

		if ((dataLength+freeIndex+18)<254)//continuamos paquete actual
		{
			currentPack.ReadByte(0,nf);
			nf++;
			currentPack.WriteByte(0,nf);
			for (i=0;i<(18+dataLength);i++)
			{
				currentPack.WriteByte(freeIndex++,pFrame[i]);
			}
			return false;
		}
		else //nuevo paquete
		{

			for (;freeIndex<254;freeIndex++)
			{
				currentPack.WriteByte(freeIndex,0);
			}
			deliveryPack=currentPack;
			freeIndex=1;

			if (get_free_pack_list(currentPack))
			{

				currentPack.WriteByte(0,1);//NF
				for (i=0;i<(18+dataLength);i++)
				{
					currentPack.WriteByte(freeIndex++,pFrame[i]);
				}
			}
			else
			{
				emptyPackList=true;
			}
			return true;
		}
	}
	else
	{
		return false;
	}

}


bool_t CGlobalSensorTMStore::is_empty()
{
	return emptyPackList;
}


void CGlobalSensorTCStore::set_pack_pool_memory(PackPool * pPackPool,word16_t numOfPackets)
{
	uint16_t i=0;
	uint16_t j=0;
	CPackHandler packAux;


	index=1;
	pPackPool->AllocPackages(numOfPackets,storePackList);

	nPacks=0;
	storePackList.ToHeadPack();
	numOfPackets=storePackList.GetPackCounter();//puede que sea menos del deseado,
	//nPacks=0;
	//por eso se vuelve a comprobar
	for (i=0;i<numOfPackets;i++)
	{
		storePackList.GetCurrentEditPack(packAux);
		for (j=0;j<256;j++)
		{
			packAux.WriteByte(j,0);
		}
		storePackList.NextEditPack();
	}

}

void CGlobalSensorTCStore::add_sgi_sen_tc_pack(CPackHandler  pack)
{
	CPackHandler packAux;
	uint16_t i=0;
	byte_t newByte=0;

	nPacks++;
	storePackList.GetCurrentEditPack(packAux);
	for (i=0;i<256;i++)
	{
		pack.ReadByte(i,newByte);
		packAux.WriteByte(i,newByte);
	}
	storePackList.NextEditPack();



}

bool_t CGlobalSensorTCStore::get_ssi_data_frame(SSI_data_frame_t & dataFrame)
{
	CPackHandler packAux;
	uint16_t i=0;
	uint8_t length=0;

	storePackList.ToHeadPack();
	do
	{
		storePackList.GetCurrentReadPack(packAux);
		packAux.ReadByte(0,length);
	}
	while((!length) && storePackList.NextReadPack());

	if (length)//list is not empty
	{
		//Get Data Frame
		if (length>32)
		{
			for (i=0;i<32;i++)
			{
				packAux.ReadByte((i+index),dataFrame.data[i]);
			}
			packAux.WriteByte(0,(length-32));
		}
		else
		{
			for (i=0;i<length;i++)
			{
				packAux.ReadByte((i+index),dataFrame.data[i]);
			}
			packAux.WriteByte(0,0);
			nPacks--;
		}
		dataFrame.length=i;
		index+=i;
		if (index>253)
		{
			index=1;
			for (i=0;i<256;i++)
			{
				packAux.WriteByte(i,0);
			}
		}

		dataFrame.sensor_id=MASK_BIDIRECTIONAL_SSI;
		dataFrame.flags=MASK_DATA_SSI;

		return true;
	}

	else//list is empty
	{

			return false;

	}

}



